package com.smartfactory.sail.controller;


import com.ada.base.common.ApiResponse;
import com.ada.base.common.ControllerResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.smartfactory.sail.config.OperRecord;
import com.smartfactory.sail.entity.Outwork;
import com.smartfactory.sail.entity.StorageRecord;
import com.smartfactory.sail.entity.enums.OutWordStatusEnum;
import com.smartfactory.sail.entity.enums.StorageStatusEnum;
import com.smartfactory.sail.mapper.StorageRecordMapper;
import com.smartfactory.sail.service.IOutworkService;
import com.smartfactory.sail.service.IStorageCurrentInfoService;
import com.smartfactory.sail.service.IStorageRecordService;
import com.smartfactory.sail.utils.CodeUtils;
import com.smartfactory.sail.vo.outwork.InstorageReq;
import com.smartfactory.sail.vo.outwork.OutWorkInfoReq;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 王巨壁
 * @since 2019-08-28
 */
@RestController
@RequestMapping("/outwork")
@Api(tags = "外发")
public class OutworkController  extends ControllerResponse {

    private IStorageRecordService iStorageRecordService;
    private StorageRecordMapper storageRecordMapper;
    private IOutworkService iOutworkService;
    private IStorageCurrentInfoService iStorageCurrentInfoService;

    public OutworkController(IStorageRecordService iStorageRecordService, StorageRecordMapper storageRecordMapper, IOutworkService iOutworkService, IStorageCurrentInfoService iStorageCurrentInfoService) {
        this.iStorageRecordService = iStorageRecordService;
        this.storageRecordMapper = storageRecordMapper;
        this.iOutworkService = iOutworkService;
        this.iStorageCurrentInfoService = iStorageCurrentInfoService;
    }

    @ApiOperation(value = "获取待外发列表 可加条件 可不加",notes = "获取待外发的入库列表 可加条件 可不加")
    @PostMapping("waitOutWorkList")
    public ApiResponse<List<StorageRecord>> getToOutWorkList(@RequestBody Map<String,Object> param) {
        QueryWrapper<StorageRecord> query = new QueryWrapper<>();
        query.allEq(param);
        return ok(storageRecordMapper.getToOutWorkList(query));
    }

    @ApiOperation(value = "获取外发列表",notes = "获取外发列表")
    @PostMapping("getInfoList")
    public ApiResponse<List<Outwork>> getInfoList(@RequestBody Map<String,Object> param) {
        QueryWrapper<Outwork> query = new QueryWrapper<>();
        query.allEq(param).orderByAsc("status");
       return  ok(iOutworkService.list(query));
    }

    @OperRecord(content = "外发",level = 1)
    @ApiOperation(value = "提交外发列表", notes = "提交外发列表")
    @PostMapping("commit/{custName}")
    public ApiResponse<Object> getInfoList(@PathVariable("custName")String custName, @RequestBody List<OutWorkInfoReq> req) {

        List<Outwork> outworks = new ArrayList<>();
        for (OutWorkInfoReq outWorkInfoReq : req) {
            Outwork outwork = new Outwork();
            outwork.setBatchId(CodeUtils.creatWFCode());
            outwork.setStorageRecordId(outWorkInfoReq.getStorageRecordId());
            outwork.setOutWorkSize(outWorkInfoReq.getOutWorkSize());
            outwork.setCustName(custName);
            outworks.add(outwork);
            @NotNull Long storageRecordId = outWorkInfoReq.getStorageRecordId();
            StorageRecord byId = iStorageRecordService.getById(storageRecordId);

            //修改流水状态 .
            UpdateWrapper<StorageRecord> update = new UpdateWrapper<>();
            update.eq("id",storageRecordId);
            update.set("status", StorageStatusEnum.WF);
            iStorageRecordService.update(update);
            //减少对应的库存信息
            iStorageCurrentInfoService.updateSize(storageRecordId, -1);//减法
        }
        if (iOutworkService.saveBatch(outworks)) {
            return ok();
        } else {
            return fail("提交失败");
        }
    }

    @OperRecord(content = "入库",level = 1)
    @ApiModelProperty("入库")
    @PostMapping("inStorage")
    public ApiResponse<Object> inStorage(@RequestBody List<InstorageReq> instorageReqs) {

        QueryWrapper<Outwork> queryWrapper = new QueryWrapper<>();
        List<Long> ids = new ArrayList<>();
        instorageReqs.forEach(instorageReq->ids.add(instorageReq.getId()));
        if (ids.size() == 0) {
            return fail("请选择之后再提交!");
        }
        queryWrapper.in("id", ids);
        List<StorageRecord> records = new ArrayList<>();
        List<Outwork> outworks = iOutworkService.list(queryWrapper);
        for (Outwork outwork : outworks) {
            for (InstorageReq instorageReq : instorageReqs) {
                if (instorageReq.getId() == outwork.getId()) {
                    StorageRecord storageRecord = new StorageRecord();
                    storageRecord.setId(outwork.getStorageRecordId());
                    storageRecord.setSize(instorageReq.getSize());
                    storageRecord.setStatus(StorageStatusEnum.DRK);
                    records.add(storageRecord);
                    outwork.setInStorageSize(outwork.getInStorageSize() + instorageReq.getSize());
                }
            }

            outwork.setStatus(OutWordStatusEnum.YCL);
        }
        iOutworkService.updateBatchById(outworks);
        return ok(iStorageRecordService.updateBatchById(records));
    }

    @GetMapping("getAllCompanys")
    @ApiOperation(value = "获取所有历史外发企业名称",notes = "获取所有外发企业名称")
    public ApiResponse<List<String>> getAllWFCompany() {
        return ok(iOutworkService.getAllCompanys());
    }
}
